<template>
  <div class="search-box">
    <div class="header-bar-box">
      <div class="header-bar clear">
        <img src="../../assets/search/a2.png" alt="" class="ico1">
        <input type="text" class="search" id="search" placeholder="输入品牌或关键字" v-model="searchWord" @blur="searchInput" @keyup.13="searchInput">
        <img src="../../assets/search/delete.png" alt="" class="delete" @click="clearInput">
      </div>
    </div>

    <div class="default-result">
      <div class="hot-tt">热门搜索</div>
      <div class="hot-box">

        <span class="link_search" v-for="item in hotList" v-on:click="redirect('/search/search-all?keyword='+item.keyword)">{{item.keyword}}</span>

      </div>
      <div class="local-font" v-if="showHistory">
        <div class="local-tt">最近搜索 <span class="delete" v-on:click="clearStore"></span></div>
        <div class="local-list">
          <div class="list">
            <span class="link_search" v-for="item in searchHistory" v-on:click="redirect('/search/search-all?keyword='+item)"> {{item}} </span>
          </div>
        </div>

      </div>
    </div>

    <!--<div class="search-result">-->
      <!--<div class="shop-box">-->
        <!--<div class="sub-tt">商品</div>-->
        <!--<ul class="shop-result">-->
          <!--<li class="clearFix shop">-->
            <!--<img class="icon fl" src="https://dummyimage.com/90x90"/>-->
            <!--<div class="goods fl">-->
              <!--<p>【9.9新】GUCCI 斜挎包</p>-->
              <!--<p>￥8,800.00</p>-->
            <!--</div>-->
          <!--</li>-->
          <!--<li class="clearFix shop">-->
            <!--<img class="icon fl" src="https://dummyimage.com/90x90"/>-->
            <!--<div class="goods fl">-->
              <!--<p>【9.9新】GUCCI 斜挎包</p>-->
              <!--<p>￥8,800.00</p>-->
            <!--</div>-->
          <!--</li>-->
        <!--</ul>-->
        <!--<div class="more"><a href="#">查看更多商品 ></a></div>-->
      <!--</div>-->
      <!--<div class="artice-box">-->
        <!--<div class="sub-tt">文章</div>-->
        <!--<ul class="artice-result">-->
          <!--<li class="clearfix artice">-->
            <!--<div class="fl">-->
              <!--<img src="https://dummyimage.com/90x90" alt="">-->
            <!--</div>-->
            <!--<div class="fr">-->
              <!--<div class="ms">测试数据测试测试数据</div>-->
              <!--<div class="time-box">-->
                <!--<span class="time">2016-12-29</span>-->
                <!--<img src="../../assets/search/a3.png" class="follow" alt="" /><i>11</i>-->
                <!--<img src="../../assets/search/a4.png" class="pinglun" alt="" /><i>111</i>-->
              <!--</div>-->
            <!--</div>-->
          <!--</li>-->
          <!--<li class="clearfix artice">-->
            <!--<div class="fl">-->
              <!--<img src="https://dummyimage.com/90x90" alt="">-->
            <!--</div>-->
            <!--<div class="fr">-->
              <!--<div class="ms">测试数据测试数据测试数据测试数据测试数据测试数据测试数据</div>-->
              <!--<div class="time-box">-->
                <!--<span class="time">2016-12-29</span>-->
                <!--<img src="../../assets/search/a3.png" class="follow" alt="" /><i>11</i>-->
                <!--<img src="../../assets/search/a4.png" class="pinglun" alt="" /><i>111</i>-->
              <!--</div>-->
            <!--</div>-->
          <!--</li>-->
        <!--</ul>-->
        <!--<div class="more"><a href="#">查看更多商品 ></a></div>-->
      <!--</div>-->
      <!--<div class="user-box">-->
        <!--<div class="sub-tt">用户</div>-->
        <!--<ul class="user-result">-->
          <!--<li class="clearFix user">-->
            <!--<img class="icon fl" src="https://dummyimage.com/30x30"/>-->
            <!--<p class="name fl">用户名1</p>-->
          <!--</li>-->
          <!--<li class="clearFix user">-->
            <!--<img class="icon fl" src="https://dummyimage.com/30x30"/>-->
            <!--<p class="name fl">用户名2</p>-->
          <!--</li>-->
          <!--<li class="clearFix user">-->
            <!--<img class="icon fl" src="https://dummyimage.com/30x30"/>-->
            <!--<p class="name fl">用户名1</p>-->
          <!--</li>-->
        <!--</ul>-->
        <!--<div class="more"><a href="#">查看更多用户 ></a></div>-->
      <!--</div>-->
    <!--</div>-->

    <!--<div class="no-result">-->
      <!--<div class="no">-->
        <!--<img src="../../assets/search/a5.png" alt="" />-->
      <!--</div>-->
      <!--<div class="no-text">没有相关信息</div>-->
    <!--</div>-->

  </div>
</template>

<script>

export default {
  name: 'Search',
  data () {
    return {
      hotList:[],
      searchHistory:[],
      showHistory:false,
      searchWord:''
    }
  },
  created:function(){
    // $('search').focus();
  },
  mounted:function(){
    var _this = this;

    this.initData();



    _this.searchHistory = localStorage.getItem('searchHistory') && JSON.parse(localStorage.getItem('searchHistory')) || [];

    if(_this.searchHistory.length>0){
      _this.showHistory = true;
    }

//    $('body').on('blur','#search',function(){
//
//      if($('#search').val() == ''){
//        return;
//      }
//      _this.searchHistory.push($('#search').val());
//
//      localStorage.setItem('searchHistory',JSON.stringify(_this.searchHistory));
//
////      alert('发送请求');
//      _this.$router.push({path:'/search/search-all?keyword='+ $(this).val()});
////      _this.$router.push({path:'/'});
//
//    })
  },
  methods:{
    searchInput(){
      if(this.searchWord==''){
        return;
      }
      this.searchHistory.push(this.searchWord);
      localStorage.setItem('searchHistory',JSON.stringify(this.searchHistory));
      this.$router.push({path:'/search/search-all?keyword='+this.searchWord});

    },
    initData:function(){

      var _this = this;
      //hotSearch
      var ajaxData = {};
      ajaxData.limit = 10;
      $.ajax({
        type:'post',
        url: RouteMap.hotSearch,
        data:ajaxData,
        success:function(res){
          if(res.code == 200){

            _this.hotList = res.data.hotList;
            console.log(res);

          }
          else{
            //Toast(res.msg)
          }
        }
      })
    },
    clearStore:function(){
      this.searchHistory = [];
      localStorage.setItem('searchHistory',JSON.stringify(this.searchHistory));
      this.showHistory = false;
    },
    clearInput(){
//      $('#search').val() = '';
      this.searchWord='';
    },
    redirect:function(path){
      this.$router.push(path);
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

  .default-result{
    display: -none;
  }
  .search-box{ padding-top: 0.5rem;}
  .hot-tt{
    color: #b01f24;
    text-align: center;
    padding-top: 1rem;
    font-size: 0.6rem;
  }
  .local-tt{
    color: #b01f24;
    text-align: center;
    margin-top: 1rem;
    font-size: 0.6rem;
    position: relative;
  }
  .local-tt .delete{
    display: inline-block;
    width: 0.5rem;
    height: 0.5rem;
    background: url("../../assets/search/a1.png");
    background-size: 100% 100%;
    position: absolute;
    right: 1rem;
    top:0.2rem;

  }

  .hot-box,.local-list{ padding: .33rem; }
  .header-bar-box{
    /*border-bottom: 1px solid #eee;*/
  }
  .link_search,.link_local{
    display: inline-block;
    padding: .1rem .8rem .2rem;
    width:2.3rem;
    height: .7rem;
    line-height: .9rem;
    text-align: center;
    overflow: hidden;
    background: #f5f5f5;
    margin:0.16rem .6rem;
    border-radius: 0.6rem;
    color: #707070;
  }
  .header-bar{ margin: .33rem 1rem;
    /*background: #f5f5f5;*/
    /*border-radius: 1rem; */
    padding: 0.05rem;
    border-bottom: 1px solid #1c1c1c;
  }
  .header-bar .search{ width: 11.2rem; padding-left: .2rem; font-size: 0.6rem;}
  .header-bar .ico1{ width: 0.65rem; margin-left: 0.4rem; }
  .header-bar .delete{ width: 0.7rem; display: -none;}
  .header-bar img,.header-bar input{
    vertical-align: middle;
  }

  .search-result{
    padding: 0 0.5rem;
    padding-bottom: 2rem;
    display: none;
  }
  .sub-tt{
    padding: 0.5rem 0 0.3rem;
    border-bottom: 1px solid #eee;
    color: #b01f24;
  }


  .shop{
    width:100%;
    box-sizing:border-box;
    padding:.4rem 0 .4rem .5rem;
    border-bottom: 1px solid #eee;
  }
  .shop .icon{
    width:3rem;
    height:3.6rem;

  }
  .shop .goods{
    line-height:1.2rem;
    padding-left:.5rem;
  }
  .more{
    text-align: right;
    padding-top: 0.2rem;
  }
  .more a{
    color: #707070;

  }
  .user{
    width:100%;
    box-sizing:border-box;
    padding:.4rem 0 .4rem .5rem;
    border-bottom: 1px solid #eee;
  }
  .user .icon{
    border-radius: 50%;
    width:1.2rem;
    height:1.2rem;

  }
  .user .name{
    line-height:1.2rem;
    padding-left:.5rem;
  }
  .artice{
    padding: 0.4rem 0;
    border-bottom: 1px solid #eee;
  }
  .artice .fl{
    width: 5rem;
  }
  .artice .fl img{
    width: 5rem;
    height: 3.5rem;
  }
  .artice .fr{
    width: 9.3rem;
    height: 3.5rem;
    font-size: 0.6rem;
  }
  .artice .fr .ms{
    margin-top: 0.5rem;
  }
  .time-box{
    margin-top: 0.2rem;
  }
  .time-box span,.time-box i,.time-box img{
    vertical-align: middle;
  }
  .time-box .time{
    width: 5rem;
    display: inline-block;
  }
  .time-box .follow{
    display: inline-block;
    width: 0.6rem;
    margin-right: 0.2rem;
  }
  .time-box .pinglun{
    display: inline-block;
    width: 0.5rem;
    margin-right: 0.2rem;
    margin-left: 0.2rem;
  }

  .no-result{
    text-align: center;
    color: #dedede;
    font-size: 0.5rem;
    padding: 6rem 0;
  }
  .no-result .no img{
    width: 2rem;
    padding-bottom: .5rem;
  }


</style>
